#include "main.h"
#include "ParsePAT.h"
#include "ParseTS_Head.h"
#include "ParseType.h"

TS_PAT TSpatTable(TS_PAT TS_PackagePAT, unsigned char PATtable[204])
{
    int PAT_Length = 0;

	TS_PackagePAT.table_id = PATtable[0];
	TS_PackagePAT.section_syntax_indicator = PATtable[1] >> 7;
	TS_PackagePAT.zero = (PATtable[1] >> 6) & 0x1;
	TS_PackagePAT.reserved_1 = (PATtable[1] >> 4) & 0x3;
	TS_PackagePAT.section_length = ((PATtable[1] & 0x0F) << 8) | PATtable[2];
	TS_PackagePAT.transport_stream_id = (PATtable[3] << 8) | PATtable[4];
	TS_PackagePAT.reserved_2 = PATtable[5] >> 6;
	TS_PackagePAT.version_number = (PATtable[5] >> 1) & 0x1F;
	TS_PackagePAT.current_next_indicator = (PATtable[5] << 7) >> 7;
	TS_PackagePAT.section_number = PATtable[6];
	TS_PackagePAT.last_section_number = PATtable[7];
	PAT_Length = 3 + TS_PackagePAT.section_length;
	TS_PackagePAT.CRC_32 = (PATtable[PAT_Length - 4] << 24) | (PATtable[PAT_Length - 3] << 16)
	        | (PATtable[PAT_Length - 2] << 8) | PATtable[PAT_Length - 1];

    return TS_PackagePAT;
}


TS_PAT *ParseTS_PAT(int TsPackage_len, TS_Package_Head TS_PackagePATHead)
{

    unsigned char PATtable[204] = {0};
    int PAT_Length = 0;
    int PatProgramPosition = 0; 
    int PAT_ProgramCount = 0;
    TS_PAT TS_PackagePAT = {0};
    TS_PAT *TS_PackagePatPoint;

    if(TS_PackagePATHead.payload_unit_start_indicator == 1)
    {
        memcpy(PATtable, TSdata+5, TsPackage_len-5);
        
        TS_PackagePAT = TSpatTable(TS_PackagePAT, PATtable);
    }

    printf("------------------------PAT---------------------------\n");
    printf("talble_id = %x\n",TS_PackagePAT.table_id);
    printf("section_syntax_indicator = %x\n",TS_PackagePAT.section_syntax_indicator);
    printf("zero = %x\n",TS_PackagePAT.zero);
    printf("reserved_1 = %x\n",TS_PackagePAT.reserved_1);
    printf("section_length = %x\n",TS_PackagePAT.section_length);
    printf("transport_stream_id = %x\n",TS_PackagePAT.transport_stream_id);
    printf("reserved_2 = %x\n",TS_PackagePAT.reserved_2);
    printf("version_number = %x\n",TS_PackagePAT.version_number);
    printf("current_next_indicator = %x\n",TS_PackagePAT.current_next_indicator);
    printf("section_number = %x\n",TS_PackagePAT.section_number);
    printf("last_section_numberr = %x\n",TS_PackagePAT.last_section_number);
    printf("CRC_32  = %x\n",TS_PackagePAT.CRC_32);

    PAT_Length = 3 + TS_PackagePAT.section_length;

	for(PatProgramPosition = 8; PatProgramPosition  < PAT_Length - 4; PatProgramPosition  += 4)
	{
		if (0x00 == ((PATtable[PatProgramPosition ] << 8) | PATtable[1 + PatProgramPosition ]))
		{
			TS_PackagePAT.network_PID = ((PATtable[2 + PatProgramPosition ] & 0x1F) << 8) | PATtable[3 + PatProgramPosition ];
			printf("the network_PID %0x\n", TS_PackagePAT.network_PID);
		}
		else
		{
			TS_PackagePAT.astPAT_Program[PAT_ProgramCount].program_number = (PATtable[PatProgramPosition ] << 8)
			        | PATtable[1 + PatProgramPosition ];
			TS_PackagePAT.astPAT_Program[PAT_ProgramCount].reserved_3 = PATtable[2 + PatProgramPosition ] >> 5;
			TS_PackagePAT.astPAT_Program[PAT_ProgramCount].program_map_PID = ((PATtable[2 + PatProgramPosition ] & 0x1F) << 8)
			        | PATtable[3 + PatProgramPosition ]; 
            PAT_ProgramCount++;
		}
	}

    PAT_ProgramCount = 0;

    do
    {
        printf("---------------------PATPROGRAM--------------------------\n");
        printf("PATProgram_number = %x\n",TS_PackagePAT.astPAT_Program[PAT_ProgramCount].program_number);
        printf("PATprogram_map_PID = %x\n",TS_PackagePAT.astPAT_Program[PAT_ProgramCount].program_map_PID);
        
        PAT_ProgramCount++;
    } while ((TS_PackagePAT.astPAT_Program[PAT_ProgramCount - 1].program_number !=
                TS_PackagePAT.astPAT_Program[PAT_ProgramCount].program_number) &&
                    TS_PackagePAT.astPAT_Program[PAT_ProgramCount].program_number != 0);
    
    TS_PackagePatPoint = &TS_PackagePAT;


    return TS_PackagePatPoint;
}